package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.RespiratorySystemInfoDao;
import com.jyxd.web.data.basic.RespiratorySystemInfo;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 呼吸系统信息（护理单一呼吸系统用）
 */
@Service("RespiratorySystemInfoService")
@Transactional
public class RespiratorySystemInfoService {

    @Autowired
    private RespiratorySystemInfoDao respiratorySystemInfoDao;

    public HttpResult insert (RespiratorySystemInfo respiratorySystemInfo) {
        if (Objects.isNull(respiratorySystemInfo)) {
            return new HttpResult(200, "参数不能为空");
        }
        if (Objects.isNull(respiratorySystemInfo.getPatientId())) {
            return new HttpResult(200, "患者id不能为空");
        }
        RespiratorySystemInfo respiratorySystem = respiratorySystemInfoDao.queryData(respiratorySystemInfo.getPatientId());
        if (Objects.nonNull(respiratorySystem)){
            respiratorySystem.setRespiratorySystem(respiratorySystemInfo.getRespiratorySystem());
            respiratorySystemInfoDao.update(respiratorySystem);
            return new HttpResult(200, "修改成功");
        }
        respiratorySystemInfo.setId(UUIDUtil.getUUID());
        respiratorySystemInfo.setCreateTime(new Date());
        boolean insert = respiratorySystemInfoDao.insert(respiratorySystemInfo);
        if (insert) {
            return new HttpResult(200, "保存成功");
        } else {
            return new HttpResult(200, "保存失败");
        }
    }

    public HttpResult update (RespiratorySystemInfo respiratorySystemInfo) {
        if (Objects.isNull(respiratorySystemInfo)) {
            return new HttpResult(200, "参数不能为空");
        }
        if (Objects.isNull(respiratorySystemInfo.getPatientId())) {
            return new HttpResult(200, "患者id不能为空");
        }
        if (Objects.isNull(respiratorySystemInfo.getId())) {
            return new HttpResult(200, "主键id不能为空");
        }
        RespiratorySystemInfo respiratorySystem = respiratorySystemInfoDao.queryData(respiratorySystemInfo.getId());
        respiratorySystem.setRespiratorySystem(respiratorySystemInfo.getRespiratorySystem());
        respiratorySystem.setStatus(respiratorySystemInfo.getStatus());
        boolean update = respiratorySystemInfoDao.update(respiratorySystem);
        return update ? new HttpResult(200, "更新成功")
                : new HttpResult(200, "更新失败");
    }

    public HttpResult queryData (Map<String, Object> map) {
        if (CollectionUtils.isEmpty(map)) {
            return new HttpResult(200, "参数不能为空");
        }
        if (Objects.isNull(map.get("patientId"))) {
            return new HttpResult(200, "患者id不能为空");
        }
        RespiratorySystemInfo respiratorySystemInfo = respiratorySystemInfoDao.queryData((String) map.get("patientId"));
        return new HttpResult(200, respiratorySystemInfo, "查询成功");
    }

    public HttpResult queryList (Map<String, Object> map) {
        if (CollectionUtils.isEmpty(map))
            return new HttpResult(200, "参数不能为空");
        List<RespiratorySystemInfo> respiratorySystemInfos = respiratorySystemInfoDao.queryList(map);
        if (CollectionUtils.isEmpty(respiratorySystemInfos))
            return new HttpResult(200, "暂无数据");
        return new HttpResult(200, respiratorySystemInfos, "查询成功");
    }


}
